package com.dms.common.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 密码工具类
 */
public class PasswordUtil {

    /**
     * 生成密码哈希
     * @param password 明文密码
     * @return 加密后的密码哈希
     */
    public static String encode(String password) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(10);
        return encoder.encode(password);
    }

    /**
     * 验证密码
     * @param rawPassword 明文密码
     * @param encodedPassword 加密后的密码
     * @return 是否匹配
     */
    public static boolean matches(String rawPassword, String encodedPassword) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(10);
        return encoder.matches(rawPassword, encodedPassword);
    }
    
    /**
     * 主方法，用于在命令行生成密码哈希
     */
    public static void main(String[] args) {
        String password = "123456";
        String encoded = encode(password);
        System.out.println("密码: " + password);
        System.out.println("哈希: " + encoded);
        System.out.println("验证: " + matches(password, encoded));
    }
} 